//
//  LZLNewFeatureViewController.swift
//  swiftDemo
//
//  Created by luozhilin on 2018/9/13.
//  Copyright © 2018年 luozhilin. All rights reserved.
//

import UIKit

class LZLNewFeatureViewController: LZLBaseView {
    // 要传入的数组
    var imageArray:[String]!
    // 滚动的主视图
    fileprivate var newFeatureScroll:UIScrollView!
    // UIPageControl
    private var imagePageControl:UIPageControl!
    
    init(imageWithArray:[String], isHiddenSkipBtn:Bool) {
        
        let frame = CGRect(x: 0, y: 0, width: YYScreenWidth, height: YYScreenHeigth)
        super.init(frame: frame)
        self.imageArray = imageWithArray;
        // 程序健壮性的判断
        if imageWithArray.count==0 || imageWithArray.count<0 {
            return
        }
        
        self.addScrollView(frame: frame);
        self.addPageControl()
        self.addSkipButton(isHiddenSkipBtn: isHiddenSkipBtn)
        self.addEachImage()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func skipBtnAction() {
        
        UIView.animate(withDuration: 1, animations: {
            self.alpha = 0
        }) { (finish) in
            self.removeFromSuperview()
        }
    }
    
}


// 功能的抽离扩展
extension LZLNewFeatureViewController {
    
    fileprivate func addScrollView(frame: CGRect) {
        let scrollView = UIScrollView(frame: frame)
        scrollView.delegate = self;
        self.newFeatureScroll = scrollView;
        scrollView.showsVerticalScrollIndicator = false;
        scrollView.showsHorizontalScrollIndicator = false;
        scrollView.isPagingEnabled = true
        scrollView.backgroundColor = UIColor.lightGray
        scrollView.bounces = false
        // 在这里的imageArray一定是有值的
        scrollView.contentSize = CGSize(width: (CGFloat)(self.imageArray.count)*YYScreenWidth, height: YYScreenHeigth)
        self.addSubview(scrollView)
        
    }
    
    fileprivate func addSkipButton(isHiddenSkipBtn:Bool) -> Void{
        // 如果是隐藏按钮，就不做任何处理，直接返回
        if isHiddenSkipBtn {
            return
        }
        
        let skipBtn = UIButton(frame: CGRect(x: YYScreenWidth * 0.8, y: YYScreenHeigth * 0.1, width: 70, height: 35))
        skipBtn.setTitle("跳过", for: .normal)
        skipBtn.setTitleColor(UIColor.white, for: .normal)
        skipBtn.backgroundColor = UIColor.gray
        skipBtn.layer.cornerRadius = self.bounds.size.height*0.5
        skipBtn.addTarget(self, action: #selector(skipBtnAction), for: .touchUpInside)
        self.addSubview(skipBtn)
    }
    
    fileprivate func addPageControl() {
        
        let pageControl = UIPageControl(frame: CGRect(x: 0, y: YYScreenHeigth*0.9, width: YYScreenWidth, height: YYScreenHeigth*0.1))
        self.addSubview(pageControl)
        self.imagePageControl = pageControl
        pageControl.numberOfPages = self.imageArray.count
        pageControl.currentPage = 0;
        pageControl.pageIndicatorTintColor = UIColor.gray
        pageControl.currentPageIndicatorTintColor = UIColor.white
    }
    
    private func addEachImage()  {
        
        for i in 0..<imageArray.count {
            let imagView = UIImageView(frame: CGRect(x: YYScreenWidth*(CGFloat)(i), y: 0, width: YYScreenWidth, height: YYScreenHeigth))
            self.newFeatureScroll.addSubview(imagView)
            imagView.image = UIImage.init(named: imageArray[i])
        
        
        // 如果是在最后一张就添加立即体验几个字
            
            if i==imageArray.count-1{
                let startButton = UIButton(frame: CGRect(x: YYScreenWidth*0.1, y: YYScreenHeigth*0.8, width: YYScreenWidth*0.8, height: YYScreenHeigth*0.08))
                imagView.addSubview(startButton);
                startButton.setTitle("开始体验", for: .normal)
                startButton.setTitleColor(UIColor.white, for: .normal)
                startButton.setBackgroundImage(UIImage.init(named: "guide_btn_bg"), for: .normal)
                startButton.addTarget(self, action: #selector(skipBtnAction), for: .touchUpInside)
            }
            
        }
        
    }
}

extension LZLNewFeatureViewController: UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.frame.size.width
        self.imagePageControl?.currentPage = Int(page)
    }
    
}
